<template>
<div>
  <basic-container>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>数据</el-breadcrumb-item>
      <el-breadcrumb-item>广告</el-breadcrumb-item>
      <el-breadcrumb-item>广告位数据 - 按时</el-breadcrumb-item>
    </el-breadcrumb>
  </basic-container>
  <basic-container>
    <avue-crud 
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange">
        <!-- 自定义按钮 -->
        <template slot-scope="" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
    </avue-crud>
  </basic-container>
</div>
</template>

<script>
import { handleExcelCat } from "@/api/exportCommon";
import {
  getAdPositionList,
  getAdProviderList
} from "@/api/cat/ad";
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { adData as baseURL } from "@/api/cat/baseUrls";
export default {
  
  async created() {
    let response = await getAdPositionList();
    this.option.column[1].dicData = response.data.source;

    let {data} = await getAdProviderList();
    this.option.column[2].dicData = data.source;
  },
  data () {
    return {
      loading:false,
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "statistics_time&DESC,ad_position&ASC,ad_type&DESC",
      },
      option: {
        index: true,
        indexLabel: "编号",
        "searchBtn": true,
        "addBtn": false,
        "editBtn": false,
        "viewBtn": false,
        "delBtn": false,
        "menu": false,
        "excelBtn": false,
        "column": [
          {
            "label": "时间",
            "prop": "statistics_time",
            "type": "datetimerange",
            "search": "true",
            "span": 24,
            sortable:true,
            width:"140px"
          },

          {
            label: "广告位",
            prop: "ad_position",
            type: "select",
            dicData: [],
            props: {
              label: "advertiser_type",
              value: "id",
            }
          },
          {
            label: "广告主",
            prop: "ad_type",
            type: "select",
            dicData: [],
            props: {
              label: "ad_provider_name",
              value: "ad_type",
            }
          },
          {
            "label": "请求PV",
            "prop": "request_pv",
            "type": "input",
            sortable:true
          },
          {
            "label": "请求UV",
            "prop": "request_uv",
            "type": "input",
            sortable:true
          },
          {
            "label": "曝光PV",
            "prop": "actual_display_pv",
            "type": "input",
            sortable:true
          },
          {
            "label": "曝光UV",
            "prop": "actual_display_uv",
            "type": "input",
            sortable:true
          },
          {
            "label": "实际展示PV",
            "prop": "exposure_pv",
            "type": "input",
            sortable:true,
            width:"110px"
          },
          {
            "label": "实际展示UV",
            "prop": "exposure_uv",
            "type": "input",
            sortable:true,
            width:"110px"
          },
          {
            "label": "点击PV",
            "prop": "click_pv",
            "type": "input",
            sortable:true
          },
          {
            "label": "点击UV",
            "prop": "click_uv",
            "type": "input",
            sortable:true
          }
        ]
      }
    }
  },
  methods: {
    //数据导出
     async handleExcel() {
       this.loading = true;
       //处理搜索条件
       let params = {...this.$refs.crud.searchForm};
       params.statistics_time = `${params.statistics_time ? "!<>!" + params.statistics_time : ""}`

       //调用导出接口
       handleExcelCat(
         this.$export,
         baseURL.getAdHourList,
         { ...params, ...this.default },
         "广告位数据-按时",
         this.page.total,
         [...this.option.column],
         {
           handleIndexArr:[1,2],
           selectNameArr:["ad_position","ad_type"],
           sourceNameArr:["id","ad_type"],
           sourceValueArr:["advertiser_type","ad_provider_name"]
         }
       );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getAdHourList,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    } /**刷新 */,
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      
      console.info('tag', params.statistics_time)
      
      // params.statistics_time = `${params.statistics_time ? "!<>!" + (this.dateFormat('YYYY-mm-dd HH:MM:SS',params.statistics_time[0])+","+(this.dateFormat('YYYY-mm-dd HH:MM:SS',params.statistics_time[1]))) : ""}`
      // params.statistics_time = `${params.statistics_time ? "!<>!" + (this.dateFormat('YYYY-mm-dd HH:MM:SS',params.statistics_time[0])+","+(this.dateFormat('YYYY-mm-dd HH:MM:SS',params.statistics_time[1]))) : ""}`
      params.statistics_time = `${params.statistics_time ? "!<>!" + params.statistics_time : ""}`

      console.info('tag', params.statistics_time)
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
    }
    return fmt;
}
  },
}
</script>